﻿using System;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Data;
using SLS.Controls;
using SLS.ExClassLib.Event;
namespace SLS.Base
{
    public partial class ExVIP : SLBC
    {
        public CollectionViewSource vwMasterTable
        {
            get
            {
                return (CollectionViewSource)(BaseData.TryFindResource("vwBaseTable"));
            }
        }
        public SLBC BaseData
        {
            get
            {
                SLBC exc = (SLBC)FindName("BaseData");
                return exc;
            }
        }
        public FilteringDataGrid MainGridData
        {
            get 
            {
                if (BaseData != null)
                {
                    return (FilteringDataGrid)BaseData.FindName("MainGridData");
                }
                else
                    return null;
            }
        }
        public void RestoreDataFromDisk()
        {
            DataSet setTemp = new DataSet();
            try
            {
                if (HasXMLDataStore && File.Exists(ExControlXMLPath))
                {
                    setTemp.ReadXml(ExControlXMLPath);
                }

                if (setTemp != null)
                {
                    foreach (DataTable tbTemp in setTemp.Tables)
                    {
                        string TBName = tbTemp.TableName;
                        #region Merge Data into the UI
                        if (!TBName.Contains("QueryCond") && SetUI.Tables.Contains(TBName))
                        {
                            SetUI.Tables[TBName].Clear();
                            SetUI.Merge(tbTemp);
                            //setUI = (SLDBSET)DataAccessService.Instance.ImportDataTable(setUI, tbTemp);
                        }
                        #endregion
                    }
                }
                if (HasXMLDataStore && File.Exists(QueryCondXMLPath))
                {
                    setTemp = new DataSet();
                    setTemp.ReadXml(QueryCondXMLPath);
                }
                if (setTemp != null)
                {
                    foreach (DataTable tbTemp in setTemp.Tables)
                    {
                        string TBName = tbTemp.TableName;
                        #region Merge Data into the UI
                        if (TBName.Contains("QueryCond"))
                        {
                            if (ViewDataSet.SetQuery.Tables.Contains(TBName))
                            {
                                ViewDataSet.SetQuery.Tables[TBName].Clear();
                                ViewDataSet.SetQuery.Merge(tbTemp);
                            }
                        }
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
